//
//  CCI.m
//  AppFinance
//
//  Created by wei on 12-7-22.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "AFCCI.h"
#import <math.h>

@implementation AFCCI

static AFCCI *_kpiCCI = nil;

static float _kpiMaxCCI = 0;
static float _kpiMinCCI = 0;

static float _kpiLastCCI = 0;

+(AFCCI*) app{
    if (_kpiCCI == nil) {
        _kpiCCI = [[self alloc] init];
    }
    return _kpiCCI;
}

+(float) kpiMaxCCI{
    return _kpiMaxCCI;
}
+(float) kpiMinCCI{
    return _kpiMinCCI;
}

+(float) kpiLastCCI{
    return _kpiLastCCI;
}

int cci_iParam[] = {14};

-(void) initKlineArray:(NSArray *)_kLineArray{
    kLineArray = _kLineArray;
    
    _cciArray = nil;
    _maArray = nil;
    
    _kpiMaxCCI = -10000000;
    _kpiMinCCI = 10000000;
}

-(void) cciData{
    
    if(kLineArray == nil || [kLineArray count] <= 0){
        return;
    }
    
    int n = cci_iParam[0];
    if(n > [kLineArray count] || n < 2){
        return;
    }
    
    _cciArray = [NSMutableArray array];
    _maArray = [NSMutableArray array];
    
    NSDictionary *dict;
    float high = 0;
    float low = 0;
    float close = 0;
    float f_3 = 3.0;
    
    double sum = 0.0;
    for(int i = 0; i < n - 1; i++){
        dict = [kLineArray objectAtIndex:0];
        high = [[dict objectForKey:AF_K_MARKET_HIGH] floatValue];
        low = [[dict objectForKey:AF_K_MARKET_LOW] floatValue];
        close = [[dict objectForKey:AF_K_MARKET_CLOSE] floatValue];
        sum += (high + low + close) / f_3;
        
        [_cciArray addObject:[NSNumber numberWithFloat:0]];
        [_maArray addObject:[NSNumber numberWithFloat:0]];
    }
    
    int _kLineCount = (int)[kLineArray count];
    float prec = 0.0f;
    float ma = 0;
    for(int i = n - 1; i < _kLineCount; i++) {
        dict = [kLineArray objectAtIndex:i];
        high = [[dict objectForKey:AF_K_MARKET_HIGH] floatValue];
        low = [[dict objectForKey:AF_K_MARKET_LOW] floatValue];
        close = [[dict objectForKey:AF_K_MARKET_CLOSE] floatValue];
        
        sum -= prec;
        sum += (high + low + close) / f_3;
        
        ma = (float)(sum / (double)n);
        [_maArray addObject:[NSNumber numberWithFloat:ma]];
        
        // 计算前一个值
        dict = [kLineArray objectAtIndex:(i - n + 1)];
        high = [[dict objectForKey:AF_K_MARKET_HIGH] floatValue];
        low = [[dict objectForKey:AF_K_MARKET_LOW] floatValue];
        close = [[dict objectForKey:AF_K_MARKET_CLOSE] floatValue];
        prec = (high + low + close) / f_3;
    }
    
    float cci = 0;
    double dp = 0.014999999999999999;
    for(int i = n - 1; i < _kLineCount; i++) {
        sum = 0.0;
        ma = [[_maArray objectAtIndex:i] floatValue];
        for(int j = (i - n + 1); j <= i; j++){
            dict = [kLineArray objectAtIndex:j];
            high = [[dict objectForKey:AF_K_MARKET_HIGH] floatValue];
            low = [[dict objectForKey:AF_K_MARKET_LOW] floatValue];
            close = [[dict objectForKey:AF_K_MARKET_CLOSE] floatValue];
            
            sum +=  fabs((high + low + close) / f_3 - ma);
        }
        if(sum == 0.0){
            cci = [[_cciArray objectAtIndex:(i-1)] floatValue];
        }
        else{
            dict = [kLineArray objectAtIndex:i];
            high = [[dict objectForKey:AF_K_MARKET_HIGH] floatValue];
            low = [[dict objectForKey:AF_K_MARKET_LOW] floatValue];
            close = [[dict objectForKey:AF_K_MARKET_CLOSE] floatValue];
            cci = (float)((double)((high + low + close) / f_3 - ma) / ((dp * sum) / (double)n));
        }
        
        if (_kpiMinCCI > cci) {
            _kpiMinCCI = cci;
        }
        if (_kpiMaxCCI < cci) {
            _kpiMaxCCI = cci;
        }
        
        [_cciArray addObject:[NSNumber numberWithFloat:cci]];
    }
}

-(NSMutableArray *) kCCIArray{
    return _cciArray;
}
@end
